<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>

    <title>开发后台主页</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="css/bootstrap.css" rel="stylesheet">

    <script type="text/javascript" src="js/jquery-3.1.1.js"></script>
    <script type="text/javascript" src="js/bootstrap.js"></script>
</head>

<body>
<div class="container">
    <div class="row">
        <div class="col-md-4">
            <h2>后台API页面</h2>
            <span id="span_ver" class="label label-info"></span>
        </div>
        <div class="col-md-8">
            <div class="pull-right" id="login_panel">
                <form class="form-inline" role="form">
                    <div class="form-group">
                        <label class="sr-only" for="username">用户名</label>
                        <input type="text" class="form-control input-sm" id="username" placeholder="请输入用户名" value="whp">
                    </div>
                    <div class="form-group">
                        <label class="sr-only" for="password">密码</label>
                        <input type="password" class="form-control input-sm" id="password" placeholder="请输入密码" value="111111">
                    </div>
                    <button type="button" id="btnLogin" class="btn btn-default btn-sm">登录</button>
                </form>
            </div>
            <div class="pull-right" id="user_panel">
                <span class="label label-success">当前用户</span>
                <span id="user_span" class="label label-info"></span>
                <button type="button" id="btnLogout" class="btn btn-default btn-sm">退出</button>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-4">
            <ul>
                <li><a href="swagger-ui.html" target="_blank">swagger API 文档</a></li>
                <li><a href="druid/" target="_blank">数据源管理器(用户名：root 密码：111111)</a></li>

                <li><a href="admin/autoconfig" target="_blank">Spring 自动配置</a></li>
                <li><a href="admin/configprops" target="_blank">Spring 属性配置</a></li>
                <li><a href="admin/beans" target="_blank">Spring beans</a></li>
                <li><a href="admin/dump" target="_blank">Spring 线程栈</a></li>
                <li><a href="admin/env" target="_blank">Spring 环境变量</a></li>
                <li><a href="admin/health" target="_blank">Spring 健康指标</a></li>
                <li><a href="admin/info" target="_blank">Spring 应用信息</a></li>
                <li><a href="admin/mappings" target="_blank">Spring url映射</a></li>
                <li><a href="admin/trace" target="_blank">Spring 追踪信息</a></li>
                <li><a href="http://172.16.9.82:8881/" target="_blank">单元测试报告</a></li>
                <li>-----------------------------------</li>
                <li><a href="http://54.222.140.220:81/" target="_blank">dev前端_81</a></li>
                <li><a href="http://54.222.140.220:8081/" target="_blank">dev后端_8081</a></li>
                <li><a href="http://54.222.235.82:81/" target="_blank">master前端_80</a></li>
                <li><a href="http://54.222.235.82:8081/" target="_blank">master后端_8080</a></li>
                <li>-----------------------------------</li>
            </ul>

        </div>
        <div class="col-md-8">

        </div>
    </div>
</div>
<script type="text/javascript">
    jQuery(document).ready(function ($) {
        $("#btnLogin").click(function () {
            loginAction();
        });
        $("#btnLogout").click(function () {
            logoutAction();
        });
        checkOnline();
        showVersion();
    });

    function checkOnline() {
        $.ajax({
            url: "api/v1/context",
            type:"GET",
            cache:false,
            dataType: 'json',
            success:function (response) {
                if (response.errorCode == 401) {
                    showUserPanel(null);
                } else {
                    showUserPanel(response.data.username);
                }
            }
        });
    }

    function showUserPanel(username) {
        if(username ==null) { // show login panel
            $("#login_panel").show();
            $("#user_panel").hide();
        } else { // user panel
            $("#login_panel").hide();
            $("#user_panel").show();
            $("#user_span").text(username);
        }
    }

    function loginAction(){
        var username =  $("#username").val();
        var password = $("#password").val();
        var data = {"userName":username,"passWord":password};
        $.post({
            url: "dev/login",
            type:"POST",
            contentType:"application/json",
            processData: false,
            dataType: 'json',
            data:JSON.stringify(data),
            success:function (response) {
                if (response.errorCode == 0) {
                    showUserPanel(response.data);
                } else {
                    alert("登录失败-" + response.msg);
                }
            }
        });
    }
    function logoutAction() {
        $.get("dev/logout", function () {
            showUserPanel(null);
        });
    }
    
    function showVersion() {
        $.get("api/v1/about", function (response) {
            var version = response.name + "(version:"+response.version+", build on:"+response.date+")";
            $("#span_ver").text(version);
        });
    }


</script>

</body>
</html>